<?php
class Default_Form_ValidateSubMenu {
	
	// save errors info
	protected $_messagesError = null;
	
	// save data after validate
	protected $_arrData;
	
	public function __construct($arrParam = array(),$options = null) {
		
		// check Category Name
		
		if($arrParam['action'] == 'add') {
			$options = array('table'=>'sub_menu','field'=>'name');
		}else {
			if($arrParam['action'] == 'edit') {
				$clause = ' id !=' . $arrParam['id'];
				$options = array('table'=>'sub_menu','field'=>'name', 'exclude' => $clause);
			}
		}
		
		$validator = new Zend_Validate();
		
		// check name
		$validator->addValidator(new Zend_Validate_NotEmpty(),true)
				  ->addValidator(new Zend_Validate_StringLength(3,32),true)
				  ->addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
		
		if(!$validator->isValid($arrParam['menu_name'])) {
			$message = $validator->getMessages();
			$this->_messagesError['menu_name'] = 'Menu name: ' . current($message);
			$arrParam['menu_name'] = '';
		}
		
		// check order form
	    $validator = new Zend_Validate();
		
		$validator->addValidator(new Zend_Validate_Int(), true);
		$validator->addValidator(new Zend_Validate_GreaterThan(0), true);
		
	    if(!$validator->isValid($arrParam['order'])) {
			$message = $validator->getMessages();
			$this->_messagesError['order'] = 'order: ' . current($message);
			$arrParam['order'] = '';
		} 
		
		// save data user type in the form
		$this->_arrData = $arrParam;
	}
	
	
	// check errors
	public function isError() {
		if(count($this->_messagesError) > 0) {
			return true;
		}else {
			return false;
		}
	}
	
	// get errors
	public function getMessageError() {
		return $this->_messagesError;
	}
	
	// get data
	public function getData() {
		return $this->_arrData;
	}
}